package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.Log;
import org.example.domain.po.*;
import org.example.enums.BusinessType;
import org.example.service.ErpAccountService;
import org.example.service.ErpDepotHeadService;
import org.example.service.impl.ErpAccountServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basic/account")
public class ErpAccountController {
    @Autowired
    private ErpAccountServiceImpl accountService;
    @RequestMapping("/list")
    public R<List<ErpAccount>> list() {
        return R.success(accountService.findaccount());
    }

    @Autowired
    private ErpAccountService erpAccountService;

    @Autowired
    private ErpDepotHeadService headService;

    /**
     * 分页条件查询
     * @param current
     * @param size
     * @param user
     * @param name
     * @param accNumber
     * @param remark
     * @return
     */
    @Log(operation = "角色", businessType = BusinessType.INSERT, isSaveRequestData = true, paramName = "accNumber")
    @GetMapping("/find")
    public Result<Page<ErpAccount>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size,
            @RequestAttribute("user") ErpUser user,
            String name,
            String accNumber,
            String remark
    ){
        Page<ErpAccount> page = new Page<>(current, size);
        page = erpAccountService.findByPage(page, name, accNumber, remark, user.getTenantId());
        return Result.success(page);
    }

    /**
     * 账户新增
     * @param user
     * @param erpAccount
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestAttribute("user") ErpUser user, @RequestBody ErpAccount erpAccount){
        erpAccount.setTenantId(Math.toIntExact(user.getTenantId()));
        return erpAccountService.save(erpAccount) ? Result.success("新增成功！") : Result.error("新增失败！");
    }

    /**
     * 修改
     * @param erpAccount
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody ErpAccount erpAccount){
        return erpAccountService.updateById(erpAccount) ? Result.success("编辑成功！") : Result.error("编辑失败！");
    }

    /**
     * 根据id删除
     * @param accId
     * @return
     */
    @DeleteMapping("/{accId}")
    public Result<String> remove(@PathVariable("accId") Integer accId,
                                 @RequestAttribute("user") ErpUser user){
        QueryWrapper<ErpDepotHead> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id",accId);
        wrapper.eq("delete_flag",0);
        wrapper.eq("tenant_id",user.getTenantId());
        List<ErpDepotHead> list = headService.list(wrapper);
        if (list.size() > 0){
            return Result.success("有值");
        }
        erpAccountService.removeById(accId);
        return Result.success("删除成功！");
    }

    /**
     * 设置默认
     * @param accId
     * @return
     */
    @PutMapping("/SetDefault/{accId}")
    public Result SetDefault(@PathVariable("accId") Integer accId){
        boolean b = erpAccountService.SetDefault(accId);
        return b ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 批量删除
     * @param erpAccounts
     * @return
     */
    @PostMapping("/batch")
    public Result removeAll(@RequestBody List<ErpAccount> erpAccounts){
        return erpAccountService.removeBatchByIds(erpAccounts) ? Result.success( "操作成功！") : Result.error("操作失败！");
    }

    /**
     * 批量修改
     * @param modify
     * @return
     */
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpAccountService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 查询启用
     * @param user
     * @return
     */
    @GetMapping("/findByStatus")
    public Result<List<ErpAccount>> findByEnabled(@RequestAttribute("user") ErpUser user) {
        QueryWrapper<ErpAccount> wrapper = new QueryWrapper<>();
        //查询状态为启用的
        wrapper.eq("status",1);
        wrapper.eq("delete_flag",0);
        wrapper.eq("tenant_id",user.getTenantId());
        List<ErpAccount> list = erpAccountService.list(wrapper);
        return Result.success(list);
    }
}
